package cn.krund.lyc.hotlerobot.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @包名: cn.krund.lyc.hotlerobot.utils
 * @作者: 刘育成
 * @时间: 2018/3/21  14:08
 * @org: 克路德人工智能
 * @描述: TODO
 * @fileName: DeviceIdUtil
 */

public class DeviceIdUtil {
    private static boolean isNextLine = false;
    private Context mContext;

    public DeviceIdUtil(Context context){
        mContext=context;
    }

    //通过adb命令获取mac地址
    public static String getLocalMacAddressFromBusybox(){
        String result="";
        result = callCmd("ip address show", "ether");
        if (TextUtils.isEmpty(result)){
            //获取wifimac地址失败
            return Constant.GETMACFIELD;
        }else {
            String[] str = result.split(" ");
            String ipmac = str[5];
            Log.i("macmac", "macmac---" + ipmac);
            return ipmac;
        }
    }

    private static String callCmd(String cmd, String filter) {
        String result = "";
        String line = "";
        try {
            Process proc = Runtime.getRuntime().exec(cmd);
            InputStreamReader is = new InputStreamReader(proc.getInputStream());
            BufferedReader br = new BufferedReader(is);

            //执行命令cmd，只取结果中含有filter的这一行
            while ((line = br.readLine()) != null) {

                if (isNextLine) {
                    //本行就是wifi mac所在行
                    result = line;
                    isNextLine = false;
                }
                if (line.contains("wlan0:")) {
                    isNextLine = true;
                }
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
